﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using DddCms.Domain.Repository;
using DddCms.Domain.Utility;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace DddCms.Domain.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class User
    {
        [Dependency]
        public IPasswordUtil PasswordUtil { get; set; }

        [Dependency]
        public ILoggingUtil LogWriter { get; set; }

        public int UserID { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public string PasswordQuestion { get; set; }
        public string PasswordAnswer { get; set; }
        public virtual List<Role> Roles { get; set; }


        /// <summary>
        /// Validates the password.
        /// </summary>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public virtual bool ValidatePassword(string password)
        {
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("The password is empty or null");

            if (PasswordUtil.verify(password, Password))
                return true;

            LogWriter.Verbose("User", "Connection", "Connection échoué");

            return false;
        }

        /// <summary>
        /// Gets the roles.
        /// </summary>
        /// <returns></returns>
        public virtual string[] GetRoles()
        {
            if (Roles == null) throw new ArgumentNullException("Roles for user is null");

            return Roles.Select(r => r.Name).ToArray();
        }
    }
}
